package exam.wangyi.autumn.S1;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int a = 0, b = 0;
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt();
        b = sc.nextInt();
        System.out.println(solution(String.valueOf(a), String.valueOf(b)));
    }

    private static boolean judge(String a, String b) {
        int ia = Integer.parseInt(a), ib = Integer.parseInt(b);
        if (ia == ib)
            return true;
        else if (ia > ib)
            return ia % ib == 0;
        else
            return ib % ia == 0;
    }

    private static int solution(String a, String b) {
        if (judge(a, b))
            return 0;
        Queue<String> q1 = new LinkedList<>(), q2 = new LinkedList<>();
        Map<String, Integer> m1 = new HashMap<>(), m2 = new HashMap<>();
        q1.offer(a);
        q2.offer(b);
        m1.put(a, 0);
        m2.put(b, 0);
        while (!q1.isEmpty() && !q2.isEmpty()) {
            int k = -1;
            if (q1.size() <= q2.size())
                k = bfs(q1, m1, m2);
            else
                k = bfs(q2, m2, m1);
            if (k >= 0)
                return k;
        }
        return -1;
    }

    private static int bfs(Queue<String> q, Map<String, Integer> cur, Map<String, Integer> other) {
        int size = q.size();
        for (int i=0; i<size; ++i) {
            String front = q.poll();
            int step = cur.get(front);
            for (int j=0; j<front.length(); ++j) {
                StringBuilder sb = new StringBuilder();
                for (int k=0; k<front.length(); ++k) {
                    if (k == j)
                        continue;
                    sb.append(front.charAt(k));
                }
                if (sb.length() == 0)
                    continue;
                String s = sb.toString();
                for (String key : other.keySet()) {
                    if (judge(key, s))
                        return step + 1 + other.get(key);
                }
                cur.put(s, step+1);
                q.offer(s);
            }
        }
        return -1;
    }
}